﻿// -----------------------------------------------------------------------
// <copyright file="CourseProgressRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CourseProgressRepository : AbstractRepository<CourseProgress>
    {
        public CourseProgressRepository(ContextHelper contextHelper)
        {
            base._contextHelper = contextHelper;
            base._context = _contextHelper.GetContext();
        }

        public override IQueryable<CourseProgress> GetAll()
        {
            var result = (from x in base._context.CourseProgresses
                          select new CourseProgress
                          {
                              Id = x.Id,
                              StudentCode = x.StudentCode,
                              CourseCode = x.CourseCode,
                              DateRegistered = x.DateRegistered,
                              CreditsMaximum = x.CreditsMaximum,
                              CreditsAchieved = x.CreditsAchieved,
                              Competent = x.Competent,
                              DateAchieved = x.DateAchieved,
                              CompletionTime = x.CompletionTime,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(CourseProgress entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.CourseProgress entityToAdd = new Context.CourseProgress
            {
                StudentCode = entity.StudentCode,
                CourseCode = entity.CourseCode,
                DateRegistered = entity.DateRegistered,
                CreditsMaximum = entity.CreditsMaximum,
                CreditsAchieved = entity.CreditsAchieved,
                Competent = entity.Competent,
                DateAchieved = entity.DateAchieved,
                CompletionTime = entity.CompletionTime,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.CourseProgresses.InsertOnSubmit(entityToAdd);
        }

        public override void Update(CourseProgress entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.CourseProgress entityToUpdate = base._context.CourseProgresses.Single(x => x.Id == entity.Id);

            entityToUpdate.StudentCode = entity.StudentCode;
            entityToUpdate.CourseCode = entity.CourseCode;
            entityToUpdate.DateRegistered = entity.DateRegistered;
            entityToUpdate.CreditsMaximum = entity.CreditsMaximum;
            entityToUpdate.CreditsAchieved = entity.CreditsAchieved;
            entityToUpdate.Competent = entity.Competent;
            entityToUpdate.DateAchieved = entity.DateAchieved;
            entityToUpdate.CompletionTime = entity.CompletionTime;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(CourseProgress entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.CourseProgress entityToDelete = base._context.CourseProgresses.Single(x => x.Id == entity.Id);

            base._context.CourseProgresses.DeleteOnSubmit(entityToDelete);
        }
    }
}
